package com.mocoop.biz;


import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.mocoop.activity.MocoopActivity;
import com.mocoop.commom.MyConstants;
import com.mocoop.dao.SyncDao;
import com.mocoop.dao.SyncDaoImp;
import com.mocoop.plugin.ajax.HttpTask;
import com.mocoop.task.SaveDataTask;

public class SyncDataBiz {
	private SyncDao syncDao;
	private SyncDataCallBack syncDataCallBack;
	private SyncDataCallBack syncAllDataCallBack;


	public void setSyncAllDataCallBack(SyncDataCallBack syncAllDataCallBack) {
		this.syncAllDataCallBack = syncAllDataCallBack;
	}


	public void setSyncDataCallBack(SyncDataCallBack syncDataCallBack) {
		this.syncDataCallBack = syncDataCallBack;
	}

	
	public void loadDataByList(MocoopActivity context,final  List<String> tableNames,String userID){
		syncDataCallBack = new   SyncDataCallBack() {
			private int count = 0;
			@Override
			public synchronized void onSuccess(int batchCount) {
				Log.d("RZZ","同步完成的表个数："+count);
				count++;
				if(count== tableNames.size()){
					syncAllDataCallBack.onSuccess(1);
				}
			}
		};
		for(String tableName:tableNames){
			 loadData(context, tableName,userID);
		 }; 
	}
	
	
	
	public void loadData(MocoopActivity context, final String tableName,String userID) {
		syncDao = new SyncDaoImp(context,userID);
//		String url = "http://110.80.35.58:23806/init/" + tableName + "/"
//				+ context.userID + "/" + getMaxTimeStamp(tableName) + "/100";
		String url = getURLByTableName(tableName);
		String pageSize = "999999";
		
		JSONObject jsonObject  = new JSONObject();
		try {
			jsonObject.put("userID", context.getUserID());
			jsonObject.put("pageSize", pageSize);
			jsonObject.put("lastUpdateStamp", getMaxTimeStamp(tableName));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		context.ajaxRequest(Method.POST, url, jsonObject,
				new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject response) {
						    SaveDataTask dataTask = new SaveDataTask();
						    dataTask.setSyncDataCallBack(syncDataCallBack);
						    dataTask.execute(response,syncDao,tableName);
					}
				},new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Log.d("RZZ",error.toString());
						syncDataCallBack.onSuccess(0);
					}
				},0);
	}

	private String getURLByTableName(String tableName) {
		String url = MyConstants.SERVICE_URL;
		if("QC_ORDER".equals(tableName)){
				url+=MyConstants.URL_ORDER;
		}else if("QC_PARTY".equals(tableName)){
			 url+=MyConstants.URL_PARTY;
		}else if("QC_ABNORMAL".equals(tableName)){
			url+=MyConstants.URL_ABNORMAL;
		}else if("QC_PROCESS".equals(tableName)){
			url+=MyConstants.URL_PROCESS;
		}else if("QC_REMARK".equals(tableName)){
			url+=MyConstants.URL_REMARK;
		}else if("QC_DYNAMIC".equals(tableName)){
			url+=MyConstants.URL_DYNAMIC;
		} else if("QC_MESSAGE".equals(tableName)){
			url+=MyConstants.URL_MESSAGE;
		} 
//		url+="?pageSize=100&lastUpdateTime="+getMaxTimeStamp(tableName);
		
		return url;
	}

	private String getMaxTimeStamp(String tableName) {
		String timeStamp = syncDao.findMaxTime(tableName);
		if (timeStamp == null) {
			timeStamp = "0";
		}
		return timeStamp;
	}

	public interface SyncDataCallBack {
		public void onSuccess(int batchCount);
	}
}
